#!/data/data/com.termux/files/usr/bin/bash
echo "正在请求存储权限..."
termux-setup-storage
# 创建符号链接
echo "正在设置常用共享目录的符号链接..."
ln -s /data/data/com.termux/files/home/storage/shared/Download ~/Download
ln -s /data/data/com.termux/files/home/storage/shared/Documents ~/Documents
ln -s /data/data/com.termux/files/home/storage/shared/Pictures ~/Pictures
ln -s /data/data/com.termux/files/home/storage/shared/Music ~/Music
ln -s /data/data/com.termux/files/home/storage/shared/Movies ~/Movies
ln -s /data/data/com.termux/files/home/storage/sdcard ~/sdcard
echo "常用共享目录设置完成！"
echo "正在更换加速源"
sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/termux-packages-24 stable main@' $PREFIX/etc/apt/sources.list && apt update && apt upgrade
# 更新和升级 Termux 包
echo "更新和升级 Termux 包..."
pkg update -y && pkg upgrade -y
echo "更新软件包..."
apt update
echo "安装 x11-repo..."
apt install x11-repo
echo "再次更新软件包..."
apt update
echo "安装依赖..."
apt install proot-distro && pkg install wget && pkg install git
echo "正在获取启动及配置脚本"
mkdir -p $HOME/Termux-Linux/Debian
wget -O $HOME/Termux-Linux/Debian/start-debian.sh https://gitee.com/scicat-team/termux-debian/raw/master/start-debian.sh
wget -O $HOME/Termux-Linux/sources.list https://gitee.com/scicat-team/termux-debian/raw/master/sources.list
echo "已获取到“start-debian.sh”"
osname="Debian"
folder="debian-fs"
shname="start-debian.sh"
imagedir="termux-debian"
tarball="debian-rootfs-arm64.tar.xz"

echo -e "\n正在从码云下载 Rootfs 镜像文件，请耐心等待"
git clone "https://gitee.com/sqlsec/termux-debian"
echo -e "\n下载完成 看来国内码云的速度还是可以的 2333"
echo -e "\n正在解压镜像 请耐心等待"
mkdir -p $HOME/Termux-Linux/$osname/$folder
proot --link2symlink tar -xJf $imagedir/$tarball -C $HOME/Termux-Linux/$osname/$folder || :
echo -e "\n解压完成 正在删除已下载的镜像"
rm -rf $imagedir
echo -e "\n正在优化系统设置"
mkdir -p $HOME/Termux-Linux/$osname/binds
cp $imagedir/$shname $HOME/Termux-Linux/$osname/
termux-fix-shebang $HOME/Termux-Linux/$osname/$shname
chmod +x $HOME/Termux-Linux/$osname/$shname
rm $HOME/Termux-Linux/$osname/$folder/etc/apt/sources.list
cp $imagedir/sources.list $HOME/Termux-Linux/$osname/$folder/etc/apt/
screenfetch -A "Debian" -L
echo -e "\n   Debian 安装成功"
echo -e "\n     祝您使用愉快\n"
echo "使用指令“bash /data/data/com.termux/files/home/Termux-Linux/Debian/start-debian.sh”启动debian环境"
bash /data/data/com.termux/files/home/Termux-Linux/Debian/start-debian.sh